
$.fn.comSelect = function(o){

    var _this = $(this);

    var defaults = {
        init:false,
        search:false,
        callback:null
    };
    var option  = $.extend({},defaults,o);

    var time = null;

    var headObj = _this.find('.sel-head');

    //默认初始化所有列表选中第一项
    if(option.init){
        headObj.val(_this.find('.sel-list').eq(0).text());
        _this.find("input:hidden").val(_this.find('.sel-list').eq(0).attr('data-val'))
    }
    if(option.search && !option.remote){
        headObj.removeAttr('readonly').prop('placeholder','请搜索');
        _this.on("input",".sel-head",function(e){
            var text = e.target.value;
            var pattern = new RegExp("^"+text);
            _this.find('.sel-list').each(function(index,item){
               var t = $(item).text();
                if(pattern.test(t)){
                    $(item).show()
                }else{
                    $(item).hide()
                }
            })
        })
    }

    if(option.remote){
        headObj.removeAttr('readonly').prop('placeholder','请搜索');
        _this.on("input",".sel-head",function(e){
            var text = e.target.value;
            clearTimeout(time);
            var p = new Promise(function(r,s){
                time = setTimeout(function(){
                    var data = option.remote.request(text);
                    r(data);
                },option.remote.time || 500)
            });
            p.then(function(data){
                renderList(data);
                //清除count = 0;
            })
        })
    }

    //绑定事件
    _this.on('click','.sel-list',function(){
        var that = $(this);
        var text = that.text();
        var dataVal = that.attr('data-val');
            that.closest('.com-select').find('.sel-head').val(text);
            dataVal ? that.closest('.com-select').find("input:hidden").val(dataVal) : '';
            //触发change事件
            _this.find("input:hidden").trigger('change');
            if(option.callback){
                option.callback(dataVal,text);
            }
    });

    headObj.on({
        'click':function(e){
            //body事件
            e.stopPropagation();
            var that = $(this);
            var isTrue = that.closest('.com-select').find('.sel-content').is(':hidden');
            var comSelectObj = $(".com-select");
                comSelectObj.not(_this).find('.sel-content').fadeOut(200);
                comSelectObj.not(_this).find('.sel-item').removeClass('active-arrow');

            if(isTrue){
                that.closest('.com-select').find('.sel-content').fadeIn();
            }else{
                that.closest('.com-select').find('.sel-content').fadeOut(200)
            }
            that.closest('.sel-item').toggleClass('active-arrow')
        }
    });

    //点击外部关闭所有列表显示
    if(!window.comSelectInit){
        window.comSelectInit = true;
        $(window).on('click',function(){
            var comSelectObj = $(".com-select");
            comSelectObj.find('.sel-content').fadeOut(200);
            comSelectObj.find('.sel-item').removeClass('active-arrow')
        });
    }

    function renderList(data){
        var result = "";
        data.forEach(function(item,index){
            var str = '<li data-val="'+item.value+'" class="sel-list">'+item.name+'</li>';
            result += str;
        });
        $(".sel-content").html(result).fadeIn(200);
    }

    return {
        render:function(d){
            renderList(d)
        }
    }
};

